iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
0
自我挑戰組

各種筆記系列 第 6

[Kata] Clojure - Day 6

  • 分享至 

  • xImage
  •  

Is n divisible by x and y?

Create a function that checks if a number n is divisible by two numbers x AND y. All inputs are positive, non-zero digits.
回傳 true 當 n 可以同時被 x 及 y 整除

Examples:
1) n =   3, x = 1, y = 3 =>  true because   3 is divisible by 1 and 3
2) n =  12, x = 2, y = 6 =>  true because  12 is divisible by 2 and 6
3) n = 100, x = 5, y = 3 => false because 100 is not divisible by 3
4) n =  12, x = 7, y = 5 => false because  12 is neither divisible by 7 nor 5
(ns kata.test
  (:require [clojure.test :refer :all]
            [kata         :refer [is-divisible]]))
(defn tester [a b c exp]
  (testing (str "(isDivisible " a " " b " " c ")")
    (is (= (is-divisible a b c) exp))))
(deftest basic-tests
  (tester 3 3 4 false)
  (tester 12 3 4 true)
  (tester 8 3 4 false)
  (tester 48 3 4 true)
  (tester 100 5 10 true)
  (tester 100 5 3 false)
  (tester 4 4 2 true)
  (tester 5 2 3 false)
  (tester 17 17 17 true)
  (tester 17 1 17 true))

Solution

相除之後是不是整數

(ns kata)
(defn is-divisible [n x y]
  ;; your code here
  (and (integer? (/ n x)) (integer? (/ n y)))
)

餘數是不是等於零

(ns kata)
(defn is-divisible [n x y]
  (and (= 0 (mod n x)) (= 0 (mod n y))))

上一篇
[Kata] Clojure - Day 5
下一篇
[Kata] Clojure - Day 7
系列文
各種筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言